package de.chessgame.inventory;

import java.util.List;

import de.chessgame.ChessGameMod;
import de.chessgame.gamelogic.Board;
import de.chessgame.tileentity.TileEntityChessBoard;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ICrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

public class ContainerChessBoard extends Container {
	private TileEntityChessBoard tileEntityChessBoard;
	private boolean white;

	private static final int NUMBER_OF_HORIZONTAL_SLOTS = 8;
	private static final int NUMBER_OF_VERTICAL_SLOTS = 8;
	private static final int NUMBER_OF_TOTAL_SLOTS = NUMBER_OF_HORIZONTAL_SLOTS
			* NUMBER_OF_VERTICAL_SLOTS;
	private static final int MARGIN = 8;
	private static final int DISTANCE = 0;
	private static final int CELL_SIZE = 16;

	public ContainerChessBoard(EntityPlayer player,
			TileEntityChessBoard tileEntityChessBoard) {
		this.tileEntityChessBoard = tileEntityChessBoard;

		white = !(player.getDisplayName().equals(tileEntityChessBoard
				.getPlayerOnBlack()));

		createSlots();
	}

	private void createSlots() {
		int x;
		int y;
		int id;

		// Only the ids of the slots changes, if the player is on the black
		// side.
		if (white) {
			for (int i = 0; i < NUMBER_OF_VERTICAL_SLOTS; i++) {
				for (int j = 0; j < NUMBER_OF_HORIZONTAL_SLOTS; j++) {
					id = i * NUMBER_OF_HORIZONTAL_SLOTS + j;

					x = MARGIN + DISTANCE + CELL_SIZE * j;
					y = MARGIN + DISTANCE + CELL_SIZE * i;
					this.addSlotToContainer(new Slot(tileEntityChessBoard, id,
							x, y));
				}
			}
		} else {
			for (int i = 0; i < NUMBER_OF_VERTICAL_SLOTS; i++) {
				for (int j = 0; j < NUMBER_OF_HORIZONTAL_SLOTS; j++) {
					id = NUMBER_OF_TOTAL_SLOTS
							- (i * NUMBER_OF_HORIZONTAL_SLOTS + j) - 1;
					x = MARGIN + DISTANCE + CELL_SIZE * j;
					y = MARGIN + DISTANCE + CELL_SIZE * i;
					this.addSlotToContainer(new Slot(tileEntityChessBoard, id,
							x, y));
				}
			}
		}

	}

	@Override
	public boolean canInteractWith(EntityPlayer entityPlayer) {
		return tileEntityChessBoard.isUseableByPlayer(entityPlayer);
	}

	@Override
	public ItemStack transferStackInSlot(EntityPlayer player, int slot) {
		ItemStack stack = null;
		Slot slotObject = (Slot) inventorySlots.get(slot);

		// null checks and checks if the item can be stacked (maxStackSize > 1)
		if (slotObject != null && slotObject.getHasStack()) {
			ItemStack stackInSlot = slotObject.getStack();
			stack = stackInSlot.copy();

			// merges the item into player inventory since its in the tileEntity
			if (slot < 9) {
				if (!this.mergeItemStack(stackInSlot, 0, 35, true)) {
					return null;
				}
			}
			// places it into the tileEntity is possible since its in the player
			// inventory
			else if (!this.mergeItemStack(stackInSlot, 0, 9, false)) {
				return null;
			}

			if (stackInSlot.stackSize == 0) {
				slotObject.putStack(null);
			} else {
				slotObject.onSlotChanged();
			}

			if (stackInSlot.stackSize == stack.stackSize) {
				return null;
			}
			slotObject.onPickupFromSlot(player, stackInSlot);
		}
		return stack;
	}

	@Override
	public void onContainerClosed(EntityPlayer player) {
		if (player.inventory.getItemStack() != null) {
			player.inventory.setItemStack(null);
		}
		super.onContainerClosed(player);

	}
}
